package cn.wd.user.service.impl;


import cn.wd.common.util.StrUtils;
import cn.wd.user.domain.entity.Video;
import cn.wd.user.domain.mapper.VideoMapper;
import cn.wd.user.page.PageResult;
import cn.wd.user.qo.QueryObject;
import cn.wd.user.qo.VideoQuery;
import cn.wd.user.service.IVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    private final VideoMapper videoMapper;

    public VideoServiceImpl(VideoMapper videoMapper) {
        this.videoMapper = videoMapper;
    }

    @Override
    public Video getById(String videoId) {
        return videoMapper.selectByPrimaryKey(videoId);
    }

    @Override
    public void changeStatus(Long id, String status) {
        videoMapper.changeStatus(id, status);
    }

    // @Override
    // public void updateById(Video video) {
    //     videoMapper.updateByPrimaryKey(video);
    // }
    //
    // @Override
    // public void save(Video video) {
    //     videoMapper.insert(video);
    // }

    @Override
    public void deleteById(Long id, boolean deleted) {
        videoMapper.deleteByPrimaryKey(id, deleted);
    }

    @Override
    public Page<Video> listVideoPage(VideoQuery qo) {
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<Video>()
                .eq(Video::getDeleted, qo.getDeleted())
                .eq(qo.getStatus() != null, Video::getStatus, qo.getStatus())
                .ge(StrUtils.hasText(qo.getBeginDate()), Video::getReleaseTime, qo.getBeginDate())
                .le(StrUtils.hasText(qo.getEndDate()), Video::getReleaseTime, qo.getEndDate());
        if (StrUtils.hasText(qo.getKeyword())) {
            wrapper.and(rap ->
                            rap.like(Video::getTitle, qo.getKeyword()))
                    .or()
                    .like(Video::getSubTitle, qo.getKeyword());
        }
        return videoMapper.selectCustomPage(qo, wrapper);
    }

    @Override
    public PageResult<Video> query(QueryObject qo) {
        Long total = videoMapper.selectForCount(qo);
        if (total == 0) {
            return PageResult.empty(qo.getCurrentPage(), qo.getPageSize());
        }
        List<Video> videos = videoMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), total, videos);
    }
}
